package com.lightbook.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lightbook.pojo.User;
import com.lightbook.service.UserService;
import com.lightbook.utils.RegexUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    //  注册转发
    @RequestMapping(value = "/regist", method = RequestMethod.GET)
    public String registerRepeat() {
        return "regist";
    }

    //    登录转发
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String logonRepeat(Model model) {
        model.addAttribute("fail", "请输入账号和密码");
        return "login";
    }

    //   登录密码判断
//  User selectByPhone(String userPhone);
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String loginProcessing(String userEmail, String userPassword, Model model, HttpServletResponse response) {
        if (StringUtils.isBlank(userEmail)) {
            model.addAttribute("fail", "邮箱不能为空");
            model.addAttribute("userEmail", userEmail);
            model.addAttribute("userPassword", userPassword);
            return "/login";
        } else if (!RegexUtils.checkEmail(userEmail)) {
            model.addAttribute("fail", "邮箱格式错误");
            model.addAttribute("userEmail", userEmail);
            model.addAttribute("userPassword", userPassword);
            return "/login";
        }

        if (StringUtils.isBlank(userPassword)) {
            model.addAttribute("fail", "密码不能为空");
            model.addAttribute("userEmail", userEmail);
            model.addAttribute("userPassword", userPassword);
            return "/login";
        }

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_email", userEmail);
        List<User> list = userService.list(wrapper);
        User user = new User();
        if (list.size() <= 0) {
            model.addAttribute("fail", "此邮箱尚未注册, 请先注册");
            model.addAttribute("userEmail", userEmail);
            model.addAttribute("userPassword", userPassword);
            return "/login";
        }
        user = list.get(0);
        System.out.println(user);
        if (user != null) {
            if (user.getUserPassword().equals(userPassword)) {
                Cookie cookie = new Cookie("username", user.getUserName());
                cookie.setMaxAge(30*24*60*60);  // 三十天cookie过期
                cookie.setSecure(true);         //Https 安全cookie
                cookie.setHttpOnly(true);       //不能被js访问的Cookie
                response.addCookie(cookie);

                Cookie userEmailCookie = new Cookie("userEmail", user.getUserEmail());
                userEmailCookie.setMaxAge(30*24*60*60);  // 三十天cookie过期
                userEmailCookie.setSecure(true);         //Https 安全cookie
                userEmailCookie.setHttpOnly(true);       //不能被js访问的Cookie
                response.addCookie(userEmailCookie);

                return "redirect:/index";
            } else {
                model.addAttribute("fail", "密码错误");
                model.addAttribute("userEmail", userEmail);
                model.addAttribute("userPassword", userPassword);
                return "login";
            }
        } else {
            model.addAttribute("fail", "未注册");
            return "login";
        }
    }

    //    /registProcessin
    @RequestMapping(value = "/regist", method = RequestMethod.POST)
    public String registProcessing(User user,
                                  @RequestParam("repwd") String repwd,
                                  RedirectAttributes redirectAttributes,
                                  HttpServletResponse response,
                                  Model model
                                  ) {
        // 账户已经注册
        // 字段为空
        // 检测邮箱格式
        String userName = user.getUserName();
        String userEmail = user.getUserEmail();
        String firstPassword = user.getUserPassword();
        String secondPassword = repwd;

        if (StringUtils.isBlank(userName)) {
            GiveModel(model, userName, userEmail, firstPassword, secondPassword, repwd, "用户名不能为空");
            return "/regist";
        }

        QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("user_name", userName);
        List<User> list = userService.list(queryWrapper1);
        if (list.size() > 0) {
            GiveModel(model, userName, userEmail, firstPassword, secondPassword, repwd, "该用户名已被注册");
            return "/regist";
        }

        if (StringUtils.isBlank(firstPassword)) {
            GiveModel(model, userName, userEmail, firstPassword, secondPassword, repwd, "密码不能为空");
            return "/regist";
        } else if (firstPassword.length() < 6) {
            GiveModel(model, userName, userEmail, firstPassword, secondPassword, repwd, "密码不能小于6位");
            return "/regist";
        }

        if (StringUtils.isBlank(secondPassword)) {
            GiveModel(model, userName, userEmail, firstPassword, secondPassword, repwd, "密码不能为空");
            return "/regist";
        } else if (secondPassword.length() < 6) {
            GiveModel(model, userName, userEmail, firstPassword, secondPassword, repwd, "密码不能小于6位");
            return "/regist";
        }

        if (firstPassword.equals(secondPassword)) {

            if (StringUtils.isBlank(userEmail)) {
                GiveModel(model, userName, userEmail, firstPassword, secondPassword, repwd, "邮箱不能为空");
                return "/regist";
            } else if (!RegexUtils.checkEmail(userEmail)) {
                GiveModel(model, userName, userEmail, firstPassword, secondPassword, repwd, "邮箱格式错误");
                return "/regist";
            }

            QueryWrapper<User> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("user_email", userEmail);
            List<User> list2 = userService.list(queryWrapper2);
            if (list2.size() > 0) {
                GiveModel(model, userName, userEmail, firstPassword, secondPassword, repwd, "该邮箱已被注册");
                return "/regist";
            }

            userService.save(user);

            //redirectAttributes.addFlashAttribute("username", user.getUserName());
            Cookie cookie = new Cookie("username", user.getUserName());
            cookie.setMaxAge(30*24*60*60);  // 三十天cookie过期
            cookie.setSecure(true);         //Https 安全cookie
            cookie.setHttpOnly(true);       //不能被js访问的Cookie
            response.addCookie(cookie);
            return "redirect:/login";
        } else {
            GiveModel(model, userName, userEmail, firstPassword, secondPassword, repwd, "两次输入的密码不一致");
            return "/regist";
        }
    }

    private void GiveModel(Model model, String userName, String userEmail, String firstPassword, String secondPassword, String repwd, String registMsg) {
        model.addAttribute("userName", userName);
        model.addAttribute("userEmail", userEmail);
        model.addAttribute("firstPassword", firstPassword);
        model.addAttribute("secondPassword", secondPassword);
        model.addAttribute("repwd", repwd);
        model.addAttribute("registMsg", registMsg);
    }

    // 退出账号
    @GetMapping("/signout")
    public String signOut(HttpServletResponse response) {
        // 将Cookie的值设置为null
        Cookie cookie = new Cookie("username", null);
        //将`Max-Age`设置为0
        cookie.setMaxAge(0);
        response.addCookie(cookie);

        Cookie cookieUserEmail = new Cookie("userEmail", null);
        cookieUserEmail.setMaxAge(0);
        response.addCookie(cookieUserEmail);
        return "redirect:/index";
    }
}
